<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AccountDAO
 *
 * @author Luong Trong Hieu
 */
class AccountDAO {
   public function getUser($id)
    {
        $tblAccount = new Account();
        $id = (int)$id;
        $row = $tblAccount->fetchRow('id = '. $id);
        if(!$row)
        {
            return 0;
        }
        else
        {
            return $row->toArray();
        }
    }

    public function getUserList()
    {

    }

    public function getUserPage($page, $limit)
    {
        $table = new Account();
  
        $db = $table->getAdapter();
        $where = $db->quoteInto('status = ?', Constant::$STATUS_AVAILABLE);
        
        $result = $table->fetchAll($where, "username", ($page-1)*$limit, $limit);
        return $result->toArray();
    }

    public function insertUser($data)
    {
        $table = new Account();
        if($table->fetchRow("username = '$data[username]'")
          || $table->fetchRow("email = '$data[email]'"))
            return 0;

      $table->insert($data);
      return 1;
    }

    public function updateUser($id, $data)
    {
        $table = new Account();
        $avaiStatus = Constant::$STATUS_AVAILABLE;
        if($table->fetchRow("status = $avaiStatus AND id !='$id'
                AND username = '$data[username]'")
          || $table->fetchRow("status = $avaiStatus AND id !='$id'
                AND email = '$data[email]'"))
          return 0;

        $table->update($data, 'id = '. $id);
        return 1;
    }

    public function deleteUser($id)
    {
        $table = new Account();
        $delStatus = Constant::$STATUS_DELETED;
        $data = array('status' => $delStatus);
        $table->update($data, 'id='.$id);
    }

}
?>
